{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "96d84409",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "* 字典的查询\n",
    "* 字典的删除\n",
    "* 字典的一般方法\n",
    "* 元祖\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a952a301",
   "metadata": {},
   "source": [
    "### 字典的查询\n",
    "\n",
    "* 1.in 方法查询\n",
    "* 2.get 方法获取\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "638e232e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n",
      "您查询的内容不存在\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "\n",
    "print(info)\n",
    "\n",
    "stu_id_list = []\n",
    "\n",
    "if \"stu101\" in info:\n",
    "    print(info.get(\"stu101\"))  # 查询 key 是否在字典当中 用in\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b05662d",
   "metadata": {},
   "source": [
    "### 字典的删除\n",
    "\n",
    "* 1.pop()指定删除\n",
    "    * pop() 删除 pop()不填内容  对应记忆 ==> append() 删除末位\n",
    "* 2.gpopitem()随机删除\n",
    "    \n",
    "\n",
    "* 3.remove() 使用remove之前，应该先判断该元素是否存在该list当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5ec0303",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Zhichao\" in names)\n",
    " names_user = input(\"删除内容：\")\n",
    "while True:\n",
    "     if names_user in names:\n",
    "         names.remove(names_user)\n",
    "    else:\n",
    "\n",
    "  names.remove(\"Zhicha\")\n",
    " print(\"remove之后的names：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "7e1e9337",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "81a8b301",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LiuYu'"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop('stu1803') # 注意对应的数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "17b441d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "8b5fdcd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1804', None)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "127c735c",
   "metadata": {},
   "source": [
    "### 字典的一般方法\n",
    "* A.字典的循环遍历\n",
    "* B.fromkeys()方法\n",
    "* C.setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "125a65c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c108901d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9b2fcfb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用户就是：给所有的值使用同样的操作/方法\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6d7e5c69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03b6f5b2",
   "metadata": {},
   "source": [
    "**fromkeys()方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "86c5dee0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "95b63238",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2001': 123456, 'stu2002': 123456, 'stu2003': 123456}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化字典信息\n",
    "学生初始密码 ={}\n",
    "学生初始密码.fromkeys([\"stu2001\",\"stu2002\",\"stu2003\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f61c287",
   "metadata": {},
   "source": [
    "**setdefault()方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "9216d2d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "88f33916",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "28e16fbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault(\"stu1805\",\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "a0a32fb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae40ac18",
   "metadata": {},
   "source": [
    "###  元组(tuple)\n",
    "\n",
    "* [元组](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)\n",
    "* 1. 它是和list几乎完全一样的序列数据\n",
    "* 2. 它与list不同的地方是，**不可变**（因为不可变，所以几乎没有方法）\n",
    "* 3. 元组的形式： (1,2,34,5) 对比 元组()  列表[]\n",
    "* 4. 元组是一个一旦创建就不能改变的列表。任何情况下这个元组都不能再改变。通常可以把元组想象成一个常量列表。\n",
    "* 5. 语法：\n",
    "  * 元组只有两个方法，count和index。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "57e239a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a = (3,2,1,4,5,2,2,4,5,6,4)\n",
    "print(type(a))\n",
    "print(a.index(2))\n",
    "print(a.count(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "da2c8fc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = \"zhichao\",\"youge\",\"xiaoming\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "7728c866",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('zhichao', 'youge', 'xiaoming')"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "0be42085",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "3249ebe1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.count(\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "4ef672ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.index(\"zhichao\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
